﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ScriptEngine.Web;
using ScriptEngine;

namespace ScriptEngine.Web
{
    // Extension Method e Querystring
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_Click(object sender, EventArgs e)
        {
            string message="";

            if (PythonEngine.Instance.CheckSyntax(txtCode.Text, new ScriptContext { }, new string[] { "System" }, ref message))
            {
                ScriptDataSource sds = new ScriptDataSource();

                sds.Create(new Script { ScriptCode = txtCode.Text });

                ScriptSource.DataBind();
                gvScripts.DataBind();
            }
            else
                lblResult.Text = message;
        }

        protected void btnExec_Click(object sender, EventArgs e)
        {
            HiddenField hId = (HiddenField)((Control)sender).Parent.FindControl("hId");

            Script script = new Script{Id=int.Parse(hId.Value)};

            script = new ScriptDataSource().Read(script);

            try
            {
                object result = PythonEngine.Instance.Evaluate(script.ScriptCode, new ScriptContext { }, new string[] { "System" });

                lblResult.Text = result.ToString();
            }
            catch (Exception x)
            {

                lblResult.Text = x.Message;
            }
        }
    }
}